<template>
  <div class="login_body">
    <div class="bg" />
    <div class="box">
      <div class="title">智慧园区-登录</div>
      <el-form ref="form" :model="formData" :rules="rules">
        <el-form-item label="账号" prop="username">
          <el-input v-model="formData.username" />
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input v-model="formData.password" show-password />
        </el-form-item>

        <el-form-item prop="remember">
          <el-checkbox v-model="rememberFlag">记住我</el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            class="login_btn"
            @click="loginForm"
          >登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { REMEMBER_KEY } from '@/constants/KEY'
export default {
  name: 'Login',
  data() {
    return {
      // 登录框里的用户输入数据
      formData: {
        username: '',
        password: ''
      },
      // 校验规则
      rules: {
        username: [
          { required: true, message: '必填项', trigger: 'blur' },
          { validator: this.checkUsername, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '必填项', trigger: ['blur', 'change'] }
        ]
      },
      // 校验结果
      formValidateFlag: false,
      // 得到的token
      newToken: '',
      // 记住我，按钮
      rememberFlag: true
    }
  },
  mounted() {
    // 得到本地记录的密码和账户,回显
    const userLoginInfo = JSON.parse(localStorage.getItem('userLoginInfo'))
    if (userLoginInfo) {
      Object.assign(this.formData, userLoginInfo)
    }
  },
  methods: {
    // 登录按钮的点击事件
    async loginForm() {
      // 获取表单实例，校验表单
      const form = this.$refs.form
      form.validate((value) => {
        this.formValidateFlag = value
      })
      if (!this.formValidateFlag) {
        return
      }
      // 校验表单成功，调用登录接口

      await this.$store.dispatch('user/doLogin', this.formData)
      // 记住我，选框的功能
      if (this.rememberFlag) {
        const rememberFormData = this.formData
        localStorage.setItem(REMEMBER_KEY, JSON.stringify(rememberFormData))
      } else {
        this.formData.username = ''
        this.formData.password = ''
        localStorage.removeItem(REMEMBER_KEY)
      }
      this.$message({ type: 'success', message: `登录成功` })

      // 得到地址栏上的重定向路径
      if (this.$route.query.redirect) {
        this.$router.push({ path: `${this.$route.query.redirect}` })
      } else {
        this.$router.push({ path: '/' })
      }
    },
    // 自定义校验规则
    checkUsername(rule, value, callback) {
      if (value.length < 3) {
        callback(new Error('位数大于3!'))
      } else {
        callback()
      }
    }
  }
}
</script>

<style scoped lang="scss">
.login_body {
  display: flex;
}
.bg {
  width: 60vw;
  height: 100vh;
  background: url("~@/assets/login-bg.svg") no-repeat;
  background-position: right top;
  background-size: cover;
}
.box {
  margin: 200px 10% 0;
  flex: 1;
  .title {
    padding-bottom: 76px;
    font-size: 26px;
    font-weight: 500;
    color: #1e2023;
  }
  ::v-deep() {
    .ant-form-item {
      display: flex;
      margin-bottom: 62px;
      flex-direction: column;
    }
    .ant-form-item-label label {
      font-size: 16px;
      color: #8b929d;
    }
    .ant-input,
    .ant-input-password {
      border-radius: 8px;
    }
  }
}
.login_btn {
  width: 100%;
}
</style>
